package com.bridgeintelligent.tag.engine.core.refresh;

import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.stereotype.Component;

/**
 * Created by JackCheung on 2019/10/21 8:25 PM.
 */
@Component
public class EngineRefreshApiUtils {

    private Feign.Builder builder;

    /**
     * 初始化builder
     */
    private synchronized void doInit() {
        if (null == builder) {
            builder = Feign
                    .builder()
                    .decoder(createDecoder())
                    .encoder(createEncoder());
        }
    }

    /**
     * 创建解码器
     *
     * @return
     */
    private Decoder createDecoder() {
        return new JacksonDecoder();
    }

    /**
     * 创建编码器
     *
     * @return
     */
    private Encoder createEncoder() {
        return new JacksonEncoder();
    }

    /**
     * 初始化
     */
    private void init() {
        if (null != builder) {
            return;
        }
        doInit();
    }

    /**
     * 调用刷新缓存
     *
     * @param clazz
     * @param url
     * @param <T>
     * @return
     */
    public <T> T build(Class<T> clazz, String url) {
        init();
        return builder.target(clazz, url);
    }
}
